var express = require('express');
var router = express.Router();
var Activity = require('../models/activity.js').activity;
var Address = require('../models/activity.js').address;
var mongoose = require('../models/mongodb.js');
var Comment = require('../models/activity.js').comment;
router.get('/all/',function(req, res) {
    var count = req.query.count;
        Activity.findNewWithLimit(count,function(err, doc) {
            if (err) {
                console.log(err);
                var sendObj = {
                    hasmore:0,
                    error: {
                        code:1,
                        description:'查询失败'
                    }
                };
                res.send(sendObj);
                // 查询失败
            } else {
                var result = new Array();
                console.log(doc);
                for(var item in doc) {
                    var obj = {
                        id:doc[item]._id,
                        coverImage:doc[item].images[0],
                        address:doc[item].address.address,
                        title:doc[item].title,
                        money:doc[item].money,
                        collectCount:doc[item].collected.length,
                        activityTime:doc[item].time,
                        applyCount:doc[item].people,
                        author:doc[item].author
                    }
                    result.push(obj);
                }
                // 查询成功
                var sendObj = {
                    hasmore:1,
                    error: {
                        code:0,
                        description:'查询成功'
                    },
                    data:result
                };
                res.send(sendObj);
            }
        });
    }
);
/* GET home page. */
router.get('/foot/', function(req, res) {
    var count = req.query.count;
    var activityID = req.query.start;
    console.log(req.query.count + ' ' + req.query.start)
    if (activityID) {
        var id = mongoose.Types.ObjectId(activityID);
        console.log(id);
        Activity.findByIDWithLimit(id,count,function(err, doc) {
                if (err) {
                    var sendObj = {
                        hasmore:false,
                        error: {
                            code:1,
                            description:'查询失败'
                        }
                    }
                    req.send(sendObj);
                    // 查询失败
                    console.log('查询失败');
                } else {
                    console.log('查询成功' + doc);
                    var result = new Array();
                    for(var item in doc) {
                        var obj = {
                            id:doc[item].id,
                            coverImage:doc[item].images[0],
                            address:doc[item].address.address,
                            title:doc[item].title,
                            money:doc[item].money,
                            collectCount:doc[item].collected.length,
                            activityTime:doc[item].time,
                            applyCount:doc[item].people,
                            author:doc[item].author
                        }
                        result.push(obj);
                    }
                    // 查询成功
                    var sendObj = {
                        hasmore:1,
                        error: {
                            code:0,
                            description:'查询成功'
                        },
                        data:result
                    };
                    res.send(sendObj);
                }                   // 查询成功
            })
    } else {
        console.log('参数错误');
        // 参数错误
        var sendObj = {
            hasmore:false,
            error: {
                code: 2,
                description: '参数错误'
            }
        }
        res.send(sendObj);
    }
});
router.get('/head/', function(req, res) {
    var count = req.query.count;
    var activityID = req.query.start;
    console.log(req.query.count + ' ' + req.query.start)
    if (activityID) {
        var id = mongoose.Types.ObjectId(activityID);
        console.log(id);
        Activity.findByIDWithGte(id,count,function(err, doc) {
            if (err) {
                var sendObj = {
                    hasmore:false,
                    error: {
                        code:1,
                        description:'查询失败'
                    }
                }
                req.send(sendObj);
                // 查询失败
                console.log('查询失败');
            } else {
                console.log('查询成功' + doc);
                var result = new Array();
                for(var item in doc) {
                    var obj = {
                        id:doc[item].id,
                        coverImage:doc[item].images[0],
                        address:doc[item].address.address,
                        title:doc[item].title,
                        money:doc[item].money,
                        collectCount:doc[item].collected.length,
                        activityTime:doc[item].time,
                        applyCount:doc[item].people,
                        author:doc[item].author
                    }
                    result.push(obj);
                }
                // 查询成功
                var sendObj = {
                    hasmore:1,
                    error: {
                        code:0,
                        description:'查询成功'
                    },
                    data:result
                };
                res.send(sendObj);
            }                   // 查询成功
        })
    } else {
        console.log('参数错误');
        // 参数错误
        var sendObj = {
            hasmore:false,
            error: {
                code: 2,
                description: '参数错误'
            }
        }
        res.send(sendObj);
    }
});
module.exports = router;
